package com.lgcns.andteam2.todaymenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.lgcns.andteam2.todaymenu.info.PlaceInfo;
import com.lgcns.andteam2.todaymenu.info.TodayInfo;

/**
 * 오늘 메뉴 tab을 보여주는 activity
 */
public class TodayActivity extends Activity {
    private static final String TAG = TodayActivity.class.getSimpleName(); 
    
    private String mPlaceId;
    private String mGubunCode;
    private TodayMenuAdapter mBreakfastAdapter;
    private TodayMenuAdapter mLunchAdapter;
    private TodayMenuAdapter mDinnerAdapter;
    private TodayMenuAdapter[] mAdapterList;
    
    // View
    private ViewFlipper mFlipper;
    private LinearLayout mBreakfastLayout; 
    private LinearLayout mLunchLayout; 
    private LinearLayout mDinnerLayout;
    private LinearLayout[] mMenuLayouts;
    private ListView mBreakfastList;
    private ListView mLunchList;
    private ListView mDinnerList;
    
    private int mIndex = -1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.today);
        initView();
        
        mBreakfastAdapter = new TodayMenuAdapter(this);
        mLunchAdapter = new TodayMenuAdapter(this);
        mDinnerAdapter = new TodayMenuAdapter(this);
        mAdapterList = new TodayMenuAdapter[]{mBreakfastAdapter, mLunchAdapter, mDinnerAdapter };
        
        mBreakfastList.setAdapter(mBreakfastAdapter);
        mLunchList.setAdapter(mLunchAdapter);
        mDinnerList.setAdapter(mDinnerAdapter);
    }
    
    private void initView() {
        mFlipper = (ViewFlipper)findViewById(R.id.flipper);
        mBreakfastLayout = (LinearLayout)findViewById(R.id.breakfast_layout);
        mLunchLayout = (LinearLayout)findViewById(R.id.lunch_layout);
        mDinnerLayout = (LinearLayout)findViewById(R.id.dinner_layout);
        mMenuLayouts = new LinearLayout[]{ mBreakfastLayout, mLunchLayout, mDinnerLayout };
        
        mBreakfastList = (ListView)findViewById(R.id.breakfast_list);
        mBreakfastList.setAdapter(mBreakfastAdapter);
        mLunchList = (ListView)findViewById(R.id.lunch_list);
        mLunchList.setAdapter(mLunchAdapter);
        mDinnerList = (ListView)findViewById(R.id.dinner_list);
        mDinnerList.setAdapter(mDinnerAdapter);
        
        for(int i = 0 ; i < 3 ; i++) {
            ImageButton leftArrow = (ImageButton)mMenuLayouts[i].findViewById(R.id.left_arrow);
            if(i == 0) {
                leftArrow.setVisibility(View.INVISIBLE);
            } else {
                leftArrow.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        mFlipper.setInAnimation(AnimationUtils.loadAnimation(TodayActivity.this, R.anim.push_right_in));
                        mFlipper.setOutAnimation(AnimationUtils.loadAnimation(TodayActivity.this, R.anim.push_right_out));
                        mFlipper.showPrevious();
                        mIndex--;
                    }
                });
            }
            
            ImageButton rightArrow = (ImageButton)mMenuLayouts[i].findViewById(R.id.right_arrow);
            if(i == 2) {
                rightArrow.setVisibility(View.INVISIBLE);
            } else {
                rightArrow.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        mFlipper.setInAnimation(AnimationUtils.loadAnimation(TodayActivity.this, R.anim.push_left_in));
                        mFlipper.setOutAnimation(AnimationUtils.loadAnimation(TodayActivity.this, R.anim.push_left_out));
                        mFlipper.showNext();
                        mIndex++;
                    }
                });
            }
            
            TextView gubunName = (TextView)mMenuLayouts[i].findViewById(R.id.gubun_name);
            gubunName.setText(Constants.GUBUN_NAMES[i]);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();

        String placeId = PlaceInfo.getInstance(this).getPlaceId();
        String gubunCode = TodayInfo.getTodayInfo(this).getGubunCode();
        
        if(mPlaceId == null || !mPlaceId.equals(placeId)) {
            // 새로 실행되었거나, 사업장 설정정보가 변경된 경우 새로 조회
            mPlaceId = placeId;
            refreshDataSet();
            mGubunCode = gubunCode;  
        } else if(mGubunCode != null && !mGubunCode.equals(gubunCode)) {
            // 이전에 이미 실행된 상태였고, onResume()되는 시점에 gubunCode가 바뀐(식사 시간이 변경된) 경우 새로 조회
            refreshDataSet();
            mGubunCode = gubunCode;  
        }
        
        mIndex = Integer.parseInt(mGubunCode) - 1; 
        mFlipper.setInAnimation(null);
        mFlipper.setOutAnimation(null);
        mFlipper.setDisplayedChild(mIndex);
    }
    
    /**
     * Adapter의 데이터를 갱신한다.
     */
    private void refreshDataSet() {
        String menuDate = TodayInfo.getTodayInfo(this).getDate();
        mBreakfastAdapter.refreshDataSet(menuDate, Constants.BREAKFAST_GUBUN_CODE);
        mLunchAdapter.refreshDataSet(menuDate, Constants.LUNCH_GUBUN_CODE);
        mDinnerAdapter.refreshDataSet(menuDate, Constants.DINNER_GUBUN_CODE);
        
        // empty text 표시
        for(int i = 0 ; i < 3 ; i++) {
            if(mAdapterList[i].getCount() == 0) {
                mMenuLayouts[i].findViewById(R.id.empty).setVisibility(View.VISIBLE);
            } else {
                mMenuLayouts[i].findViewById(R.id.empty).setVisibility(View.GONE);
            }
        }

    }
    
    @Override
    protected void onStart() {
        super.onStart();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    }
}
